import 'dart:convert';

import 'package:ecy_app_flutter/EcyRouter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../ecycommon/EcyCommonWidgets.dart';
import '../../../ecycommon/EcyRequestBaseManager.dart';
import '../../../ecycommon/utils/EcyUserUtil.dart';
import '../../../model/EcyApiCommonPage.dart';
import '../../../model/EcyApiCommonResponse.dart';
import '../../../model/EcyLocationModel.dart';

class EcyAfterSalePage extends EcyCommonBaseWidget {
  EcyAfterSalePage({required super.key});

  @override
  _EcyAfterSalePageState createState() {
    return _EcyAfterSalePageState(this);
  }
}

class _EcyAfterSalePageState extends EcyCommonBaseWidgetState {
  late EcyCommonBaseWidget controller;

  _EcyAfterSalePageState(EcyCommonBaseWidget vc) {
    controller = vc;
  }


  @override
  void initState() {
    super.initState();
  }

  @override
  Future<void> requestData({int pageIndex = 1, int type = 0}) async {
    super.requestData();
    bool logined = await EcyUserUtil.logined();
    Map<String, dynamic> body = {
      "pageSize": 50,
      "pageIndex": pageIndex,
      "type": type
    };
    var response =
    await EcyRequestbasemanager().get("/app/api/app/user/address/list", body);
    print("response" + response.toString());

    if (response != null) {
      var _respModel =
      EcyApiCommonResponse.fromJson(response, (response) => json);
      if (_respModel.code == 0 || _respModel.code == 200) {
        EcyApiCommonPage<EcyLocationModel> _modelPage =
        EcyApiCommonPage.fromJson(
            response["data"], (json) => EcyLocationModel.fromJson(json));
        setState(() {

        });
      }
    }
  }

  // 弹出对话框
  Future<bool?> showDeleteConfirmDialog(String content) {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          // title: const Text(''),
          contentPadding: const EdgeInsets.all(0),
          backgroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8.0),
          ),
          content: SizedBox(
            height: 130,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.only(top: 20, bottom: 30),
                  child: Text(
                    content,
                    style: TextStyle(fontSize: 18),
                  ),
                ),
                const Divider(height: 0, color: Color(0XFFdcdcdc)),
                SizedBox(
                  height: 50,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: DecoratedBox(
                          decoration: const BoxDecoration(
                              border: Border(
                                right: BorderSide(
                                  width: 1.0,
                                  color: Color(0XFFdcdcdc),
                                ),
                              )),
                          child: TextButton(
                            style: const ButtonStyle(
                              alignment: Alignment.center,
                            ),
                            child: Text(
                              '取消',
                              style: const TextStyle(
                                  fontSize: 18, color: Colors.black),
                            ),
                            onPressed: () =>
                                Navigator.of(context).pop(), // 关闭对话框
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: TextButton(
                          child: Text(
                            '确认',
                            style: const TextStyle(
                                fontSize: 18, color: Color(0xFFEA4B9B)),
                          ),
                          onPressed: () {
                            //关闭对话框并返回true
                            Navigator.of(context).pop(true);
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          actionsAlignment: MainAxisAlignment.spaceAround,
          actionsPadding: const EdgeInsets.all(0),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget image = Image.asset('assets/images/erciyuan.jpg', width: 80);

    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: const Text('申请售后', style: TextStyle(fontSize: 18),),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(8),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Card(
                color: Colors.white,
                child: Padding(
                    padding: const EdgeInsets.only(
                        left: 15, top: 15, right: 15, bottom: 15),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(
                              flex: 2,
                              child: ClipRRect(
                                  borderRadius: BorderRadius.circular(5.0),
                                  child: image),
                            ),
                            Expanded(
                                flex: 7,
                                child: Padding(
                                  padding: EdgeInsets.only(left: 6),
                                  child: Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Text('黑暗之魂手办',
                                          style: const TextStyle(
                                              fontSize: 16,
                                              color: Colors.black),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis),
                                      const Padding(
                                          padding: EdgeInsets.only(bottom: 2)),
                                      Text('谷子介绍谷子介绍谷子介绍谷子介绍谷子',
                                          style: TextStyle(
                                              fontSize: 12,
                                              color: const Color(0xFF8C8C8C)
                                                  .withOpacity(0.8)),
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis),
                                      const Padding(
                                          padding: EdgeInsets.only(bottom: 15)),
                                      Row(
                                        children: [
                                          Expanded(
                                            flex: 1,
                                            child: Text(
                                              '￥49.00',
                                              style: const TextStyle(
                                                  fontSize: 16,
                                                  color: Colors.black),
                                            ),
                                          ),
                                          Expanded(
                                            flex: 3,
                                            child: Align(
                                              alignment: Alignment.centerRight,
                                              child: Text(
                                                'x2',
                                                style: const TextStyle(
                                                    fontSize: 14,
                                                    color: Color(0xFF8C8C8C)),
                                              ),
                                            ),
                                          ),
                                        ],
                                      )
                                    ],
                                  ),
                                )),
                          ],
                        ),
                      ],
                    )),
              ),
              Padding(padding: EdgeInsets.only(bottom: 3)),
              Card(
                color: Colors.white,
                child: ListTile(
                  title: Text('换货'),
                  subtitle: Text('已收到货，需要更换已收到的货物'),
                  trailing: const Icon(Icons.arrow_forward_ios, size: 14),
                  splashColor: Colors.transparent,
                  contentPadding:EdgeInsets.only(left: 16, right: 12),
                  onTap: () {
                    EcyRouter.pushNoParams(context, EcyRouter.afterSaleDetailPage);
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

}
